using UnityEngine;
using System.Collections;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;


[ System.Serializable ]
public class SARoleConfig : Singleton< SARoleConfig > 
{
	public Dictionary< int , Dictionary< int , Dictionary< int , int > >  > Dic = new Dictionary<int, Dictionary<int, Dictionary< int , int > > >();


	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;

			// init here ,

			initConfig();
		}
	}

	
	public void load( string path )
	{
		path = GameSetting.StreamingAssetsPath + path + ".xml";

		XmlDocument xmlDoc = new XmlDocument();  
		xmlDoc.Load( path ); 

		XmlNodeList node = xmlDoc.SelectSingleNode( "root" ).ChildNodes;

		foreach( XmlElement nodeList in node )
		{
			if ( nodeList.Name == "p" )
			{
				int id = Convert.ToInt32( nodeList.GetAttribute( "id" ) );

				if ( !Dic.ContainsKey( id ) ) 
				{
					Dic[ id ] = new Dictionary<int, Dictionary<int, int> >();
				}

				Dictionary< int , Dictionary< int , int > > colorDic = Dic[ id ];
				
				for ( int i = 0 ; i < 10 ; i++ )
				{
					int id1 = Convert.ToInt32( nodeList.GetAttribute( "r" + i ) );

					for (int j = (int)SARoleColorType.COLOR0 ; j < (int)SARoleColorType.COUNT ; j++ )
					{
						if ( !colorDic.ContainsKey( j ) )
						{
							colorDic[ j ] = new Dictionary< int , int >();
						}

						Dictionary< int , int > ridingDic = colorDic[ j ];

						ridingDic[ i + (int)SARoleRidingType.BLUE_TYRANNOSAURUS ] = id1;
					}
				}

				foreach( XmlElement xe in nodeList ) 
				{
					int c = Convert.ToInt32( xe.GetAttribute( "c" ) );
					int r = Convert.ToInt32( xe.GetAttribute( "r" ) );
					int r0 = Convert.ToInt32( xe.GetAttribute( "r0" ) );
					int r1 = Convert.ToInt32( xe.GetAttribute( "r1" ) );
					
					Dictionary< int , int > ridingDic = colorDic[ c ];

					ridingDic[ (int)SARoleRidingType.NORMAL ] = r;
					ridingDic[ (int)SARoleRidingType.BRONTOSAURUS ] = r0;
					ridingDic[ (int)SARoleRidingType.TIGER ] = r1;
				}
			}


		}  

	}


	public int getActionID( int role , int color , int ride = (int)SARoleRidingType.NORMAL , int weapon = (int)SAItemType.FIST )
	{
		Dictionary< int , Dictionary< int , int > > colorDic = Dic[ role ];
		Dictionary< int , int > ridingDic = colorDic[ color ];

		if ( ride > (int)SARoleRidingType.NORMAL ) 
		{
			return ridingDic[ ride ];
		}

		return ridingDic[ ride ] + weapon;
	}

	
	public void initConfig()
	{
		load( "role" );
	}
	
	public void clearConfig()
	{
		Dic.Clear();
	}
	
	
}


